<?php
include("../../tools/session.php");
include("../../tools/controls.php");
include("../../tools/pub.php");

// 如果登录失败、无权限访问，那么采取相应措施
if (!($is_login_success === true)) { session_hop_page("../index.php"); exit(); }
if (!power_check(0)) { include("tpl-inc-wp.php"); $tpl->display($oa_tpl_path . "/no-power.html"); exit(); }

/*-- 以下是实际内容 --*/
$page_status = $_POST["page_status"];
if (!$page_status)
{
	include("tpl-inc-wp.php");
	$tpl->display($oa_tpl_path . "/default/doc_template_management.html");
}
// 如果 $page_status == 1，那么显示公文模板类型信息
elseif ($page_status == 1)
{
	include("../../tools/mysql.php");
	db_connect();
	
	header("content-type: text/xml");
	echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
	echo "<docclass>\n";
	
	$sql = "SELECT * FROM doc_tpl_class ORDER BY sequence ASC";
	$re = db_query($sql);
	
	foreach ($re as $v)
	{
		echo "<item>\n";
		echo "<classid>" . $v["tpl_class_id"] . "</classid>\n";
		echo "<enname><![CDATA[" . $v["name"] . "]]></enname>\n";
		echo "<zhname><![CDATA[" . $v["zh_name"] . "]]></zhname>\n";
		echo "<sequence><![CDATA[" . $v["sequence"] . "]]></sequence>\n";
		echo "</item>\n";
	}
	
	echo "</docclass>\n";
}
// 如果 $page_status == 2，那么显示一个公文类型下的公文列表
elseif ($page_status == 2)
{
	$classid = $_POST["classid"];
	
	include("../../tools/mysql.php");
	db_connect();
	
	header("content-type: text/xml");
	echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
	echo "<docs>\n";
	
	$sql = "SELECT * FROM doc_tpl WHERE tpl_class_id=" . $classid . " ORDER BY create_time ASC";
	$re = db_query($sql);
	
	foreach ($re as $v)
	{
		echo "<item>\n";
		echo "<tplid>" . $v["tpl_id"] . "</tplid>\n";
		echo "<name><![CDATA[" . $v["name"] . "]]></name>\n";
		echo "<template><![CDATA[" . $v["template"] . "]]></template>\n";
		echo "<type><![CDATA[" . $v["type"] . "]]></type>\n";
		echo "<author><![CDATA[" . $v["author"] . "]]></author>\n";
		echo "<createtime><![CDATA[" . $v["create_time"] . "]]></createtime>\n";
		echo "</item>\n";
	}
		
	echo "</docs>\n";
}
// 如果 $page_status == 3，那么从数据库中获取数据，并显示为 XML
elseif ($page_status == 3)
{
	$id = $_POST["id"];
	
	include ("../../tools/mysql.php");
	db_connect();
	
	header("content-type: text/xml");
	echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
	echo "<parameters>\n";
	
	$sql = "SELECT pars FROM doc_tpl WHERE tpl_id=" . $id;
	$re = db_query_once($sql);
	
	echo "<p><![CDATA[" . $re["pars"] . "]]></p>\n";
	echo "</parameters>\n";
}
// 如果 $page_status == 4，那么删除数据
elseif ($page_status == 4)
{
	$id = $_POST["id"];
	
	include("../../tools/mysql.php");
	db_connect();
	
	$sql = "DELETE FROM doc_tpl WHERE tpl_id=" . $id;
	$log_str = array("删除公文模板（ID: " . $id . "）成功！", "删除公文模板（ID: " . $id . "）失败！");
	echo check_status_write_to_log(db_exec($sql), $log_str, TRUE);
}
?>
